package com.wf.smartcomm.server.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Merchant;
import com.wf.smartcomm.server.bean.Terminal;
import com.wf.smartcomm.server.service.TerminalService;


@Service
@Transactional
public class TerminalServiceImpl implements TerminalService {
	
	@Resource
	private SessionFactory factory;

	@Override
	public void save(Terminal terminal) throws Exception {
		factory.getCurrentSession().persist(terminal);
	}

	@Override
	public Terminal getByToken(String token) throws Exception {
		Query query = factory.getCurrentSession().createQuery("from Terminal where token =:token");
		query.setParameter("token", token);
		return (Terminal) query.uniqueResult();
	}

	@Override
	public List<Terminal> list() throws Exception {
		Query query = factory.getCurrentSession().createQuery("from Terminal");
		return query.list();
	}

	@Override
	public void update(Terminal terminal) throws Exception {
		factory.getCurrentSession().merge(terminal);
	}

	@Override
	public Terminal get(String id) throws Exception {
		return (Terminal) factory.getCurrentSession().get(Terminal.class, id);
	}

	@Override
	public List<Terminal> list(Merchant merchant) throws Exception {
		Query query = factory.getCurrentSession().createQuery("from Terminal where merchant =:merchant");
		query.setParameter("merchant", merchant);
		return query.list();
	}

}
